package com.to.aboomy.bannersample;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.to.aboomy.banner.Banner;
import com.to.aboomy.banner.HolderCreator;
import com.to.aboomy.banner.IndicatorView;
import com.to.aboomy.banner.OnPageItemClickListener;
import com.to.aboomy.bannersample.pagetransformer.ScalePageTransformer;
import com.to.aboomy.bannersample.pagetransformer.NoPageTransformer;
import com.to.aboomy.bannersample.util.ToastUtil;
import com.to.aboomy.bannersample.util.Utils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.IOException;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class MultPager2BannerAbility extends Ability implements HolderCreator {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_mult_pager2_banner);
        initBanner1();
        initBanner2();

    }

    private void initBanner1() {
        Banner banner = (Banner) findComponentById(ResourceTable.Id_banner1);
        banner.setHolderCreator(this).setOuterPageChangeListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {

            }
        }).setIndicator(new IndicatorView(this)
                .setIndicatorColor(Color.GRAY).setIndicatorSelectorColor(Color.WHITE))
                .setPageMargin(-(AttrHelper.vp2px(50,getContext())),AttrHelper.vp2px(30,getContext()))
                .setPageTransformer(true, new NoPageTransformer())
                .setCornerRadius(20)
                .setOnPageClickListener(position -> ToastUtil.showToast(getContext(),"" + position))
                .setPages(Utils.getImage(2));
    }
    private void initBanner2() {
        Banner banner2 = (Banner) findComponentById(ResourceTable.Id_banner2);
        banner2.setHolderCreator(this)
                .setIndicator(((IndicatorView) findComponentById(ResourceTable.Id_indicator2))
                        .setIndicatorColor(Color.GRAY)
                        .setIndicatorStyle(IndicatorView.INDICATOR_BEZIER)
                        .setIndicatorSelectorColor(Color.RED), false)
                .setPageMargin(-(AttrHelper.vp2px(88,getContext())),AttrHelper.vp2px(37,getContext()))
                .setPageTransformer(true, new ScalePageTransformer())
                .setCornerRadius(20)
                .setOnPageClickListener(position -> ToastUtil.showToast(getContext(),"" + position))
                .setPages(Utils.getImage(2));
        banner2.setAutoPlay(true);
    }

    @Override
    public Component createView(Context context, int integer, int index) {
        Image image = new Image(context);;
        image.setScaleMode(Image.ScaleMode.STRETCH);
        image.setLayoutConfig(new ComponentContainer.LayoutConfig(MATCH_PARENT, MATCH_PARENT));
        try {
            ImageSource.SourceOptions options = new ImageSource.SourceOptions();
            options.formatHint = "image/jpg";
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            ResourceManager manager = context.getResourceManager() ;
            String path = manager.getMediaPath( integer);
            Resource r1 = manager.getRawFileEntry(path).openRawFile();
            ImageSource source = ImageSource.create(r1,options);
            PixelMap p1 = source.createPixelmap(decodingOptions);
            image.setPixelMap(p1);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return image;
    }
}
